package com.profound.blog.service;

import java.util.ArrayList;
import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.profound.blog.bean.Article;
import com.profound.blog.bean.SysArticleType;
import com.profound.blog.dao.ArticleDAO;
import com.profound.blog.dao.ArticleTypeDAO;

@Service
public class ArticleService {

	@Autowired
	private ArticleDAO dao;
	
	@Autowired
	private ArticleTypeDAO tdao;
	
	public List<Article> listByPage(int page, int size){
		List<Article> list = new ArrayList<Article>();
	    list =  dao.listPage(page, size);
	    
	    for(Article a:list){
	      String content = a.getArticleContent();
	      content = content.substring(0,content.length()>30?30:content.length());
	      a.setArticleContent(content);
	    }
	    
		return list;
	}
	
	public int getArticleCount(){
		return dao.getArticleCount();
	}
	
	public int getPageCount(int articleCount, double size){
		return (int) Math.ceil(articleCount/size);
	}
	
	public List<Article> listMyByPage(int page, int size,int blogId){
		List<Article> list = new ArrayList<Article>();
	    list =  dao.listMyPage(page, size,blogId);
	    
	    for(Article a:list){
	      String content = a.getArticleContent();
	      content = content.substring(0,content.length()>30?30:content.length());
	      a.setArticleContent(content);
	    }
	    
		return list;
	}
	
	public int getMyArticleCount(int blogId){
		return dao.getMyArticleCount(blogId);
	}
	
	public List<Article> listTypeByPage(int page, int size,int sysArticleTypeId){
		List<Article> list = new ArrayList<Article>();
	    list =  dao.listTypePage(page, size,sysArticleTypeId);
	    
	    for(Article a:list){
	      String content = a.getArticleContent();
	      content = content.substring(0,content.length()>30?30:content.length());
	      a.setArticleContent(content);
	    }
	    
		return list;
	}
	
	public int getTypeArticleCount(int sysArticleTypeId){
		return dao.getTypeArticleCount(sysArticleTypeId);
	}

	public Article showArticle(int articleId){
		return dao.findArticle(articleId);
	}
	
	public boolean publishArticle(Article a){
		boolean ok = false;
		if(1==dao.addArticle(a)){
			ok=true;
		}
		return ok;
	}
	
	public List<SysArticleType> listAllSysArticleType() {
		return this.tdao.selectSysArticleType();
	}
}
